using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Media;

namespace WP7Box2d
{
    class Audio
    {
        private int m_generalAudioIndex = -1;
        private bool m_playGeneralAudio = false;
        private List<SoundEffect> m_generalAudioList = new List<SoundEffect>();
        private List<SoundEffectInstance> m_generalAudioInstanceList = new List<SoundEffectInstance>();

        private int m_editorAudioIndex = -1;
        private bool m_playEditorAudio = false;
        private List<SoundEffect> m_editorAudioList = new List<SoundEffect>();
        private List<SoundEffectInstance> m_editorAudioInstanceList = new List<SoundEffectInstance>();

        private int m_gameAudioIndex = -1;
        private bool m_playGameAudio = false;
        private List<SoundEffect> m_gameAudioList = new List<SoundEffect>();
        private List<SoundEffectInstance> m_gameAudioInstanceList = new List<SoundEffectInstance>();

        private SoundEffect m_ammon = null;
        private SoundEffect m_argh = null;
        private SoundEffect m_hit = null;

        Random m_random = new Random();

        public Audio()
        {
        }

        private bool CanPlaySound()
        {
            if (MediaPlayer.State == MediaState.Playing)
                return false;
            else
                return true;
        }

        public void LoadContent(ContentManager cm)
        {
            if (m_generalAudioList.Count() > 0 || m_editorAudioInstanceList.Count() > 0)
                return;

            float volume = 0.7f;
            SoundEffect sound = null;
            SoundEffectInstance instance = null;

            m_ammon = cm.Load<SoundEffect>("sounds/21781__splashzooka__clap_rock");
            m_argh = cm.Load<SoundEffect>("sounds/19313__Anton__Duh");
            m_hit = cm.Load<SoundEffect>("sounds/92019__fatlane__1004-crash");

            // General loop
            sound = cm.Load<SoundEffect>("sounds/sonicfire/18564_sonicfire_ambienza_sequencer_06_120_cmaj");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_generalAudioList.Add(sound);
            m_generalAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18565_sonicfire_ambienza_sequencer_06_120_cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_generalAudioList.Add(sound);
            m_generalAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18566_sonicfire_ambienza_sequencer_07_120bpm_cmaj");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_generalAudioList.Add(sound);
            m_generalAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18567_sonicfire_ambienza_sequencer_07_120bpm_cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_generalAudioList.Add(sound);
            m_generalAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18568_sonicfire_ambienza_sequencer_08_120bpm_cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_generalAudioList.Add(sound);
            m_generalAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18569_sonicfire_ambienza_sequencer_09_120bpm_cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_generalAudioList.Add(sound);
            m_generalAudioInstanceList.Add(instance);

            // Editor loop
            sound = cm.Load<SoundEffect>("sounds/sonicfire/18552_sonicfire_ambienza_pad_01_cmaj1");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_editorAudioList.Add(sound);
            m_editorAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18553_sonicfire_ambienza_pad_01_cmaj2");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_editorAudioList.Add(sound);
            m_editorAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18554_sonicfire_ambienza_pad_01_cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_editorAudioList.Add(sound);
            m_editorAudioInstanceList.Add(instance);

            // Game loop
            sound = cm.Load<SoundEffect>("sounds/sonicfire/18537_sonicfire_ambienza_bass_line_01_Cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_gameAudioList.Add(sound);
            m_gameAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18538_sonicfire_ambienza_bass_line_02_120_Cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_gameAudioList.Add(sound);
            m_gameAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18539_sonicfire_ambienza_bass_line_03_120_Cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_gameAudioList.Add(sound);
            m_gameAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18540_sonicfire_ambienza_bass_line_04_120_Cmaj");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_gameAudioList.Add(sound);
            m_gameAudioInstanceList.Add(instance);

            sound = cm.Load<SoundEffect>("sounds/sonicfire/18541_sonicfire_ambienza_bass_line_04_120_Cmin");
            instance = sound.CreateInstance();
            instance.Volume = volume;
            m_gameAudioList.Add(sound);
            m_gameAudioInstanceList.Add(instance);
        }

        public void PlayHit()
        {
            if (CanPlaySound())
                m_hit.Play();
        }

        public void PlayAmmon()
        {
            if (CanPlaySound())
                m_ammon.Play();
        }

        public void PlayArgh()
        {
            if (CanPlaySound())
                m_argh.Play();
        }

        public void PlayGeneralAudioLoop(bool play)
        {
            m_playGeneralAudio = play;
        }

        public void PlayEditorAudioLoop(bool play)
        {
            m_playEditorAudio = play;
        }

        public void PlayGameAudioLoop(bool play)
        {
            m_playGameAudio = play;
        }

        public void Stop()
        {
            m_playEditorAudio = false;
            m_playGeneralAudio = false;
            m_playGameAudio = false;

            for (int i = 0; i < m_generalAudioInstanceList.Count(); i++)
            {
                m_generalAudioInstanceList[i].Stop();
            }
            for (int i = 0; i < m_editorAudioInstanceList.Count(); i++)
            {
                m_editorAudioInstanceList[i].Stop();
            }
            for (int i = 0; i < m_gameAudioInstanceList.Count(); i++)
            {
                m_gameAudioInstanceList[i].Stop();
            }
        }

        public void UpdateAudio()
        {
            if (m_playGeneralAudio)
            {
                if (m_generalAudioInstanceList.Count() == 0)
                    return;

                if (m_generalAudioIndex == -1)
                {
                    // First sound
                    m_generalAudioIndex = m_random.Next(0, m_generalAudioInstanceList.Count() - 1);
                    SoundEffectInstance soundInstance = m_generalAudioInstanceList[m_generalAudioIndex];
                    if (CanPlaySound())
                        soundInstance.Play();
                }
                else
                {
                    // Play rest of the list randomly
                    SoundEffectInstance soundInstance = m_generalAudioInstanceList[m_generalAudioIndex];
                    if (soundInstance.State == SoundState.Stopped)
                    {
                        m_generalAudioIndex = m_random.Next(0, m_generalAudioInstanceList.Count() - 1);
                        SoundEffectInstance soundInstanceNext = m_generalAudioInstanceList[m_generalAudioIndex];
                        if (CanPlaySound())
                            soundInstanceNext.Play();
                    }
                }
            }


            if (m_playEditorAudio)
            {
                if (m_editorAudioInstanceList.Count() == 0)
                    return;

                if (m_editorAudioIndex == -1)
                {
                    // First sound
                    m_editorAudioIndex = m_random.Next(0, m_editorAudioInstanceList.Count() - 1);
                    SoundEffectInstance soundInstance = m_editorAudioInstanceList[m_editorAudioIndex];
                    if (CanPlaySound())
                        soundInstance.Play();
                }
                else
                {
                    // Play rest of the list randomly
                    SoundEffectInstance soundInstance = m_editorAudioInstanceList[m_editorAudioIndex];
                    if (soundInstance.State == SoundState.Stopped)
                    {
                        m_editorAudioIndex = m_random.Next(0, m_editorAudioInstanceList.Count() - 1);
                        SoundEffectInstance soundInstanceNext = m_editorAudioInstanceList[m_editorAudioIndex];
                        if (CanPlaySound())
                            soundInstanceNext.Play();
                    }
                }
            }

            // Game loop
            if (m_playGameAudio)
            {
                if (m_gameAudioInstanceList.Count() == 0)
                    return;

                if (m_gameAudioIndex == -1)
                {
                    // First sound
                    m_gameAudioIndex = m_random.Next(0, m_gameAudioInstanceList.Count() - 1);
                    SoundEffectInstance soundInstance = m_gameAudioInstanceList[m_gameAudioIndex];
                    if (CanPlaySound())
                        soundInstance.Play();
                }
                else
                {
                    // Play rest of the list randomly
                    SoundEffectInstance soundInstance = m_gameAudioInstanceList[m_gameAudioIndex];
                    if (soundInstance.State == SoundState.Stopped)
                    {
                        m_gameAudioIndex = m_random.Next(0, m_gameAudioInstanceList.Count() - 1);
                        SoundEffectInstance soundInstanceNext = m_gameAudioInstanceList[m_gameAudioIndex];
                        if (CanPlaySound())
                            soundInstanceNext.Play();
                    }
                }
            }

        }

    }
}
